<?php
namespace app\common\model;

use think\Config;
use think\Db;
use think\Model;

class Order extends Model{


    protected $table = "jiujiu_order";


    public function getOwnerOrders($ownerUid,$shopId,$where){


        return Db::table('jiujiu_order')
            ->field(['jiujiu_order.*','jiujiu_user.uid','jiujiu_user.nickname','jiujiu_user.img as userImg','jiujiu_product.title','jiujiu_product.img as productImg','jiujiu_product.uid as productUid'])
            ->join('jiujiu_user','jiujiu_user.uid=jiujiu_order.buyUid','LEFT')
            ->join('jiujiu_product','jiujiu_product.id=jiujiu_order.productId','LEFT')
            ->whereOr('jiujiu_product.uid ='.$ownerUid. ' AND  '. $where)
            ->where('jiujiu_order.shopId='.$shopId. ' AND  '. $where)
            ->where('jiujiu_order.otype',0)
            ->order('jiujiu_order.id desc')
            ->paginate();


    }


    public function getInfoById($id){


        return Db::table('jiujiu_order')
        ->field(['jiujiu_order.*','jiujiu_user.uid',
            'jiujiu_user.nickname',
            'jiujiu_user.img',
            'jiujiu_product.title',
            'jiujiu_product.img as productImg',
            'jiujiu_product.salePrice',
            'jiujiu_product.agentPrice',
            'jiujiu_product.uid as productSourceUid',
            'country.cityname as country',
            'city.cityname as city',
            'district.cityname as district',
            'jiujiu_user_address.address',
            'jiujiu_user_address.name',
            'jiujiu_user_address.phone'
        ])
        ->join('jiujiu_user','jiujiu_user.uid=jiujiu_order.buyUid','LEFT')
        ->join('jiujiu_product','jiujiu_product.id=jiujiu_order.productId','LEFT')
        ->join('jiujiu_user_address','jiujiu_user_address.id=jiujiu_order.addressId','LEFT')
        ->join('jiujiu_city country','country.id=jiujiu_user_address.country','LEFT')
        ->join('jiujiu_city city','city.id=jiujiu_user_address.city','LEFT')
        ->join('jiujiu_city district','district.id=jiujiu_user_address.district','LEFT')

        ->where('jiujiu_order.id',$id)
        ->find();

    }


    public function getOwnerOrdersBySearch($ownerUid,$shopId,$where,$keyword){


        $orders1 = Db::table('jiujiu_order')
            ->field(['jiujiu_order.*','jiujiu_user.uid','jiujiu_user.nickname','jiujiu_user.img as userImg','jiujiu_product.title','jiujiu_product.img as productImg','jiujiu_product.uid as productUid'])
            ->join('jiujiu_user','jiujiu_user.uid=jiujiu_order.buyUid','LEFT')
            ->join('jiujiu_product','jiujiu_product.id=jiujiu_order.productId','LEFT')
            ->where('jiujiu_order.shopId='.$shopId. ' AND  '. $where)
            ->where('jiujiu_order.buyUid|jiujiu_order.productId|jiujiu_order.payment_order_id|jiujiu_order.id','like',$keyword)
            ->where('jiujiu_order.otype',0)
            ->order('jiujiu_order.id desc')
            ->select();
        $orders2 = Db::table('jiujiu_order')
            ->field(['jiujiu_order.*','jiujiu_user.uid','jiujiu_user.nickname','jiujiu_user.img as userImg','jiujiu_product.title','jiujiu_product.img as productImg','jiujiu_product.uid as productUid'])
            ->join('jiujiu_user','jiujiu_user.uid=jiujiu_order.buyUid','LEFT')
            ->join('jiujiu_product','jiujiu_product.id=jiujiu_order.productId','LEFT')
            ->where('jiujiu_product.uid ='.$ownerUid. ' AND  '. $where)
            ->where('jiujiu_order.buyUid|jiujiu_order.productId|jiujiu_order.payment_order_id|jiujiu_order.id','like',$keyword)
            ->where('jiujiu_order.otype',0)
            ->order('jiujiu_order.id desc')
            ->select();

        return array_merge($orders1,$orders2);

    }



    public function getUserOrder($uid,$status){
        $data = [];
        $orders = Db::table('jiujiu_order')
            ->field(['jiujiu_order.*','jiujiu_product.title','jiujiu_product.img as productImg','jiujiu_product.uid as productUid'])
            ->join('jiujiu_product','jiujiu_product.id=jiujiu_order.productId','LEFT')
            ->where('jiujiu_order.buyUid',$uid)
            ->where('jiujiu_order.status',$status)
            ->where('jiujiu_order.otype',0)
            ->order('jiujiu_order.id desc')
            ->select();

        foreach ($orders as $k=>$order){
            $data['shop'][$k] = model('shop')->where('id',$order['shopId'])->find();
        }
        $data['orders'] = $orders;
        return $data;
    }







    public function getUserWaitComment($uid){
        $data = [];
        $orders = Db::table('jiujiu_order')
            ->field(['jiujiu_order.*','jiujiu_product.title','jiujiu_product.img as productImg','jiujiu_product.uid as productUid'])
            ->join('jiujiu_product','jiujiu_product.id=jiujiu_order.productId','LEFT')
            ->where('jiujiu_order.buyUid',$uid)
            ->where('jiujiu_order.status',3)
            ->where('jiujiu_order.otype',0)
            ->order('jiujiu_order.id desc')
            ->select();
        $i=0;
        foreach ($orders as $k=>$order){
            $comment = model('comment')->where('orderId',$order['id'])->find();
            if(!$comment){
                $data['orders'][$i] = $order;
                $data['shop'][$i] = model('shop')->where('id',$order['shopId'])->find();
                $i++;
            }

        }

        return $data;
    }




    public function homeGetShopOrder($uid,$where){
        $shop = model('shop')->getOwnerShop($uid);
        $orders = $this->getOwnerOrders($uid,$shop->id,$where);
        return ['shop'=>$shop,'orders'=>$orders];
    }







   public function getShopOrderComment($uid){
        $shop = model('shop')->getOwnerShop($uid);
        $comments = Db::table('jiujiu_shop_product_comment')
            ->alias('comment')
            ->field(['comment.*',
                'jiujiu_product.title',
                'jiujiu_product.img as productImg',
                'jiujiu_user.nickname',
                'jiujiu_user.img as userImg',
                'jiujiu_order.payment_order_id'])
            ->join('jiujiu_product','jiujiu_product.id=comment.productId','LEFT')
            ->join('jiujiu_user','jiujiu_user.uid=comment.uid','LEFT')
            ->join('jiujiu_order','jiujiu_order.id=comment.orderId','LEFT')
            ->where('comment.shopId',$shop->id)
            ->select();
            return $comments;
        }



   public function sendOrder($id,$expressCompany,$expressNum){
            $order = $this->where('id',$id)->find();
            $order->expressCompany = $expressCompany;
            $order->expressNum = $expressNum;
            $order->sendTime = time();
            $order->status = 2;
            $order->save();

   }

}